import Request from "@/sdk/pocky-request/index.js"; //引入
import idfa from "@/js_sdk/dc-idfa/idfa.js";
import md5 from "js-md5";
/**
 * store 子模块
 */
const wxAd = {
	// 状态
	state: {
		accessToken: '',
		userActionId: 0
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		accessToken: (state, payload) => {
			state.accessToken = payload
		},
		userActionId: (state, payload) => {
			state.userActionId = payload
		}
	},
	// 异步方法 dispatch
	actions: {
		/**
		 * @description 判断APP是否需要行为上报
		 * @param {Object} context
		 */
		checkActionReporo(context) {
			return new Promise(resolve => {
				let _platform = uni.getSystemInfoSync().platform.toUpperCase();
				console.log(_platform);
				plus.runtime.getProperty(plus.runtime.appid, res => {
					console.log(res);
					let _vCode = parseInt(res.version.split('.').join(''));
					console.log(_vCode);
					console.log(plus.runtime.channel);
					if ((_platform == "ANDROID" && _vCode >= 115 && plus.runtime.channel=='yyb') || (_platform == 'IOS' && _vCode >= 115)) {
						resolve()
					}
				})
			})
		},

		/**
		 * @description 获取微信公众号token
		 * @param {Object} context
		 */
		getToken(context) {
			return new Promise(resolve => {
				let _now = new Date();
				// if (uni.getStorageSync('wxToken')) { //缓存中有token
				// 	let wxToken = uni.getStorageSync('wxToken');
				// 	console.log(_now.getTime() - wxToken.time);
				// 	if ((_now.getTime() - wxToken.time) > (1000 * 7000)) { //超过2小时
				// 		Request().request({
				// 			url: 'http://mp.7637.com/wx/accToken.do',
				// 			method: 'post',
				// 			data: {}
				// 		}).then(res => {
				// 			this.commit("accessToken", res.data.data.access_token);
				// 			uni.setStorageSync('wxToken', {
				// 				tokenVal: res.data.data.access_token,
				// 				time: _now.getTime()
				// 			})
				// 			resolve();
				// 		})
				// 	} else { //没有超过两小时
				// 		this.commit("accessToken", wxToken.tokenVal);
				// 		resolve();
				// 	}
				// } else { //没有缓存
					Request().request({
						url: 'http://mp.7637.com/wx/accToken.do',
						method: 'post',
						data: {}
					}).then(res => {
						console.log(res);
						this.commit("accessToken", res.data.data.access_token);
						uni.setStorageSync('wxToken', {
							tokenVal: res.data.data.access_token,
							time: _now.getTime()
						})
						resolve();
					})
				// }
			})
		},

		/**
		 * @description 上报行为
		 * @param {Object} context
		 * @param {Object} _data
		 */
		reportAction(context, _data) {
			return new Promise(resolve => {
				let _now=new Date()
				let actionSetId={
					ios:'1110517738',
					android:'1110517740'
				}
				let _postData={
					user_action_set_id:actionSetId[uni.getSystemInfoSync().platform],
					actions:[{
						action_time:parseInt(_now.getTime()/1000),
						user_id:{},
						action_type:_data.acType
					}]
				}
				if(uni.getSystemInfoSync().platform=='android'){
					let _imei='';
					if(plus.device.imei.indexOf(',')>0){
						_imei=plus.device.imei.split(',')[0]
					}else{
						_imei=plus.device.imei
					}
					_postData.actions[0].user_id.hash_imei=md5(_imei.toLowerCase());
				}else{
					_postData.actions[0].user_id.hash_idfa=md5(idfa.value().toUpperCase());
				}
				console.log(_postData);
				switch (_data.acType){
					case 'ACTIVATE_APP':{
							
						}
						break;
					case 'REGISTER':{
							_postData.actions[0].action_param={
								value:_data.userId
							}
						}
						break;
					case 'ADD_TO_CART':{
							_postData.actions[0].action_param={
								value:_data.goodId
							}
						}
						break;
					case 'PURCHASE':{
							_postData.actions[0].action_param={
								value:_data.chaseValue
							}
						}
						break;
					case 'START_APP':{
							_postData.actions[0].action_param={
								length_of_stay:1
							}
						}
						break;
					default:
						break;
				}
				console.log(_postData);
				Request().request({
					url: 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='+context.state.accessToken,
					method: 'post',
					data: _postData
				}).then(res => {
					console.log(res);
					if(_data.acType=='ACTIVATE_APP'||_data.acType=='START_APP'){
						uni.setStorageSync('lastOpen',_now.getTime())
					}

				}).catch(err=>{
					console.log(err)
				})
			})
		}
	}
}

export default wxAd
